﻿#region 头注释
/******************************************************************************
文件名称    :   Chemistry.cs
创建日期    :   2011/5/29 10:48:26
功能描述    :   化验检实体类
修改日志    :   
    2011-12-13 10:48  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 化验检实体类
    /// </summary>
    public class Chemistry : EntityBase<Chemistry, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Chemistry()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            chemistryBlood = null;
            chemistryBloodId = null;
            chemistryCruor = null;
            chemistryCruorId = null;
            chemistryUrine = null;
            chemistryUrineId = null;
            chemistryImmunity = null;
            chemistryImmunityId = null;
            chemistryLymphocyte = null;
            chemistryLymphocyteId = null;
            chemistryBiochemistry = null;
            chemistryBiochemistryId = null;
            chemistryUrine24h = null;
            chemistryUrine24hId = null;
            chemistrySperm = null;
            chemistrySpermId = null;
            chemistryPG = null;
            chemistryPGId = null;
            urineDrug = null;
            stoneDrug = null;
            ingredient = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override Chemistry CreateNew()
        {
            return new Chemistry();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.Chemistry; }
        }

        /// <summary>
        /// 获取或设置血常规
        /// </summary>
        public ChemistryBlood ChemistryBlood
        {
            get { return chemistryBlood; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryBloodId) entityState = EntityState.Changed;
                chemistryBlood = value;
            }
        }
        protected ChemistryBlood chemistryBlood;

        /// <summary>
        /// 获取或设置血常规ID
        /// </summary>
        public long? ChemistryBloodId
        {
            get { return chemistryBlood == null ? chemistryBloodId : chemistryBlood.Id; }
            set { chemistryBloodId = value; }
        }
        protected long? chemistryBloodId;

        /// <summary>
        /// 获取或设置凝血功能
        /// </summary>
        public ChemistryCruor ChemistryCruor
        {
            get { return chemistryCruor; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryCruorId) entityState = EntityState.Changed;
                chemistryCruor = value;
            }
        }
        protected ChemistryCruor chemistryCruor;

        /// <summary>
        /// 获取或设置凝血功能ID
        /// </summary>
        public long? ChemistryCruorId
        {
            get { return chemistryCruor == null ? chemistryCruorId : chemistryCruor.Id; }
            set { chemistryCruorId = value; }
        }
        protected long? chemistryCruorId;

        /// <summary>
        /// 获取或设置尿常规
        /// </summary>
        public ChemistryUrine ChemistryUrine
        {
            get { return chemistryUrine; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryUrineId) entityState = EntityState.Changed;
                chemistryUrine = value;
            }
        }
        protected ChemistryUrine chemistryUrine;

        /// <summary>
        /// 获取或设置尿常规ID
        /// </summary>
        public long? ChemistryUrineId
        {
            get { return chemistryUrine == null ? chemistryUrineId : chemistryUrine.Id; }
            set { chemistryUrineId = value; }
        }
        protected long? chemistryUrineId;

        /// <summary>
        /// 获取或设置免疫功能
        /// </summary>
        public ChemistryImmunity ChemistryImmunity
        {
            get { return chemistryImmunity; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryImmunityId) entityState = EntityState.Changed;
                chemistryImmunity = value;
            }
        }
        protected ChemistryImmunity chemistryImmunity;

        /// <summary>
        /// 获取或设置免疫功能ID
        /// </summary>
        public long? ChemistryImmunityId
        {
            get { return chemistryImmunity == null ? chemistryImmunityId : chemistryImmunity.Id; }
            set { chemistryImmunityId = value; }
        }
        protected long? chemistryImmunityId;

        /// <summary>
        /// 获取或设置淋巴细胞
        /// </summary>
        public ChemistryLymphocyte ChemistryLymphocyte
        {
            get { return chemistryLymphocyte; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryLymphocyteId) entityState = EntityState.Changed;
                chemistryLymphocyte = value;
            }
        }
        protected ChemistryLymphocyte chemistryLymphocyte;

        /// <summary>
        /// 获取或设置淋巴细胞ID
        /// </summary>
        public long? ChemistryLymphocyteId
        {
            get { return chemistryLymphocyte == null ? chemistryLymphocyteId : chemistryLymphocyte.Id; }
            set { chemistryLymphocyteId = value; }
        }
        protected long? chemistryLymphocyteId;

        /// <summary>
        /// 获取或设置血生化
        /// </summary>
        public ChemistryBiochemistry ChemistryBiochemistry
        {
            get { return chemistryBiochemistry; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryBiochemistryId) entityState = EntityState.Changed;
                chemistryBiochemistry = value;
            }
        }
        protected ChemistryBiochemistry chemistryBiochemistry;

        /// <summary>
        /// 获取或设置血生化ID
        /// </summary>
        public long? ChemistryBiochemistryId
        {
            get { return chemistryBiochemistry == null ? chemistryBiochemistryId : chemistryBiochemistry.Id; }
            set { chemistryBiochemistryId = value; }
        }
        protected long? chemistryBiochemistryId;

        /// <summary>
        /// 获取或设置24小时尿
        /// </summary>
        public ChemistryUrine24h ChemistryUrine24h
        {
            get { return chemistryUrine24h; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryUrine24hId) entityState = EntityState.Changed;
                chemistryUrine24h = value;
            }
        }
        protected ChemistryUrine24h chemistryUrine24h;

        /// <summary>
        /// 获取或设置24小时尿ID
        /// </summary>
        public long? ChemistryUrine24hId
        {
            get { return chemistryUrine24h == null ? chemistryUrine24hId : chemistryUrine24h.Id; }
            set { chemistryUrine24hId = value; }
        }
        protected long? chemistryUrine24hId;

        /// <summary>
        /// 获取或设置精液分析
        /// </summary>
        public ChemistrySperm ChemistrySperm
        {
            get { return chemistrySperm; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistrySpermId) entityState = EntityState.Changed;
                chemistrySperm = value;
            }
        }
        protected ChemistrySperm chemistrySperm;

        /// <summary>
        /// 获取或设置精液分析ID
        /// </summary>
        public long? ChemistrySpermId
        {
            get { return chemistrySperm == null ? chemistrySpermId : chemistrySperm.Id; }
            set { chemistrySpermId = value; }
        }
        protected long? chemistrySpermId;

        /// <summary>
        /// 获取或设置前列腺检查
        /// </summary>
        public ChemistryPG ChemistryPG
        {
            get { return chemistryPG; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != chemistryPGId) entityState = EntityState.Changed;
                chemistryPG = value;
            }
        }
        protected ChemistryPG chemistryPG;

        /// <summary>
        /// 获取或设置前列腺检查ID
        /// </summary>
        public long? ChemistryPGId
        {
            get { return chemistryPG == null ? chemistryPGId : chemistryPG.Id; }
            set { chemistryPGId = value; }
        }
        protected long? chemistryPGId;

        /// <summary>
        /// 获取或设置中段尿培养及药敏
        /// </summary>
        public string UrineDrug
        {
            get { return urineDrug; }
            set
            {
                if (entityState == EntityState.Saved && urineDrug != value) entityState = EntityState.Changed;
                urineDrug = value;
            }
        }
        protected string urineDrug;

        /// <summary>
        /// 获取或设置l
        /// </summary>
        public string StoneDrug
        {
            get { return stoneDrug; }
            set
            {
                if (entityState == EntityState.Saved && stoneDrug != value) entityState = EntityState.Changed;
                stoneDrug = value;
            }
        }
        protected string stoneDrug;

        /// <summary>
        /// 获取或设置结石成分分析
        /// </summary>
        public string Ingredient
        {
            get { return ingredient; }
            set
            {
                if (entityState == EntityState.Saved && ingredient != value) entityState = EntityState.Changed;
                ingredient = value;
            }
        }
        protected string ingredient;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override Chemistry CopyTo(Chemistry entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.chemistryBlood = this.chemistryBlood;
            entity.chemistryBloodId = this.chemistryBloodId;
            entity.chemistryCruor = this.chemistryCruor;
            entity.chemistryCruorId = this.chemistryCruorId;
            entity.chemistryUrine = this.chemistryUrine;
            entity.chemistryUrineId = this.chemistryUrineId;
            entity.chemistryImmunity = this.chemistryImmunity;
            entity.chemistryImmunityId = this.chemistryImmunityId;
            entity.chemistryLymphocyte = this.chemistryLymphocyte;
            entity.chemistryLymphocyteId = this.chemistryLymphocyteId;
            entity.chemistryBiochemistry = this.chemistryBiochemistry;
            entity.chemistryBiochemistryId = this.chemistryBiochemistryId;
            entity.chemistryUrine24h = this.chemistryUrine24h;
            entity.chemistryUrine24hId = this.chemistryUrine24hId;
            entity.chemistrySperm = this.chemistrySperm;
            entity.chemistrySpermId = this.chemistrySpermId;
            entity.chemistryPG = this.chemistryPG;
            entity.chemistryPGId = this.chemistryPGId;
            entity.urineDrug = this.urineDrug;
            entity.stoneDrug = this.stoneDrug;
            entity.ingredient = this.ingredient;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<Chemistry>
        {
            Chemistry entity;
            public DefaultXmlSerializer(Chemistry entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public Chemistry Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "ChemistryBloodId":
                        entity.chemistryBloodId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryCruorId":
                        entity.chemistryCruorId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryUrineId":
                        entity.chemistryUrineId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryImmunityId":
                        entity.chemistryImmunityId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryLymphocyteId":
                        entity.chemistryLymphocyteId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryBiochemistryId":
                        entity.chemistryBiochemistryId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryUrine24hId":
                        entity.chemistryUrine24hId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistrySpermId":
                        entity.chemistrySpermId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ChemistryPGId":
                        entity.chemistryPGId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "UrineDrug":
                        entity.urineDrug = reader.ReadElementString();
                        break;
                    case "StoneDrug":
                        entity.stoneDrug = reader.ReadElementString();
                        break;
                    case "Ingredient":
                        entity.ingredient = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.ChemistryBloodId != null) writer.WriteElementString("ChemistryBloodId", XmlConvert.ToString(entity.ChemistryBloodId.Value));
                if (entity.ChemistryCruorId != null) writer.WriteElementString("ChemistryCruorId", XmlConvert.ToString(entity.ChemistryCruorId.Value));
                if (entity.ChemistryUrineId != null) writer.WriteElementString("ChemistryUrineId", XmlConvert.ToString(entity.ChemistryUrineId.Value));
                if (entity.ChemistryImmunityId != null) writer.WriteElementString("ChemistryImmunityId", XmlConvert.ToString(entity.ChemistryImmunityId.Value));
                if (entity.ChemistryLymphocyteId != null) writer.WriteElementString("ChemistryLymphocyteId", XmlConvert.ToString(entity.ChemistryLymphocyteId.Value));
                if (entity.ChemistryBiochemistryId != null) writer.WriteElementString("ChemistryBiochemistryId", XmlConvert.ToString(entity.ChemistryBiochemistryId.Value));
                if (entity.ChemistryUrine24hId != null) writer.WriteElementString("ChemistryUrine24hId", XmlConvert.ToString(entity.ChemistryUrine24hId.Value));
                if (entity.ChemistrySpermId != null) writer.WriteElementString("ChemistrySpermId", XmlConvert.ToString(entity.ChemistrySpermId.Value));
                if (entity.ChemistryPGId != null) writer.WriteElementString("ChemistryPGId", XmlConvert.ToString(entity.ChemistryPGId.Value));
                if (entity.UrineDrug != null) writer.WriteElementString("UrineDrug", entity.UrineDrug);
                if (entity.StoneDrug != null) writer.WriteElementString("StoneDrug", entity.StoneDrug);
                if (entity.Ingredient != null) writer.WriteElementString("Ingredient", entity.Ingredient);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<Chemistry>
        {
            Chemistry entity;
            public DefaultJsonSerializer(Chemistry entity)
                : base()
            {
                this.entity = entity;
            }

            public Chemistry Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "chemistryBloodId":
                        entity.chemistryBloodId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryBloodId;
                        break;
                    case "chemistryCruorId":
                        entity.chemistryCruorId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryCruorId;
                        break;
                    case "chemistryUrineId":
                        entity.chemistryUrineId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryUrineId;
                        break;
                    case "chemistryImmunityId":
                        entity.chemistryImmunityId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryImmunityId;
                        break;
                    case "chemistryLymphocyteId":
                        entity.chemistryLymphocyteId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryLymphocyteId;
                        break;
                    case "chemistryBiochemistryId":
                        entity.chemistryBiochemistryId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryBiochemistryId;
                        break;
                    case "chemistryUrine24hId":
                        entity.chemistryUrine24hId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryUrine24hId;
                        break;
                    case "chemistrySpermId":
                        entity.chemistrySpermId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistrySpermId;
                        break;
                    case "chemistryPGId":
                        entity.chemistryPGId = DbConvert.ToNullableInt64(propValue) ?? entity.chemistryPGId;
                        break;
                    case "urineDrug":
                        entity.urineDrug = Convert.ToString(propValue) ?? entity.urineDrug;
                        break;
                    case "stoneDrug":
                        entity.stoneDrug = Convert.ToString(propValue) ?? entity.stoneDrug;
                        break;
                    case "ingredient":
                        entity.ingredient = Convert.ToString(propValue) ?? entity.ingredient;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //血常规ID
                writer.WritePropertyName("chemistryBloodId");
                if (entity.ChemistryBloodId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryBloodId.Value);
                }

                //凝血功能ID
                writer.WritePropertyName("chemistryCruorId");
                if (entity.ChemistryCruorId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryCruorId.Value);
                }

                //尿常规ID
                writer.WritePropertyName("chemistryUrineId");
                if (entity.ChemistryUrineId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryUrineId.Value);
                }

                //免疫功能ID
                writer.WritePropertyName("chemistryImmunityId");
                if (entity.ChemistryImmunityId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryImmunityId.Value);
                }

                //淋巴细胞ID
                writer.WritePropertyName("chemistryLymphocyteId");
                if (entity.ChemistryLymphocyteId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryLymphocyteId.Value);
                }

                //血生化ID
                writer.WritePropertyName("chemistryBiochemistryId");
                if (entity.ChemistryBiochemistryId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryBiochemistryId.Value);
                }

                //24小时尿ID
                writer.WritePropertyName("chemistryUrine24hId");
                if (entity.ChemistryUrine24hId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryUrine24hId.Value);
                }

                //精液分析ID
                writer.WritePropertyName("chemistrySpermId");
                if (entity.ChemistrySpermId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistrySpermId.Value);
                }

                //前列腺液检查ID
                writer.WritePropertyName("chemistryPGId");
                if (entity.ChemistryPGId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChemistryPGId.Value);
                }

                //中段尿培养及药敏
                writer.WritePropertyName("urineDrug");
                if (entity.UrineDrug == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.UrineDrug);
                }

                //l
                writer.WritePropertyName("stoneDrug");
                if (entity.StoneDrug == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.StoneDrug);
                }

                //结石成分分析
                writer.WritePropertyName("ingredient");
                if (entity.Ingredient == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Ingredient);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}